<?php
/*****************************************************************************
**	Файл:	Просмотр/редактирование профиля пользователя					**
**	Автор:	Dark Dayver														**
**	Версия:	1.0b															**
**	Дата:	25/12-2009														**
*****************************************************************************/
if (IN_P_ENIGMA)
{
	die('HACK!');
}

if (SERVER_NO)
{
	$template->array_data['ERROR_EMAIL'] = '';
	$template->array_data['ERROR_NAME'] = '';
	$template->array_data['ERROR_PASS'] = '';
	$template->array_data['ERROR_RE_PASS'] = '';
	$template->array_data['ERROR_LANGUAGE'] = '';
	$template->array_data['ERROR_THEME'] = '';
	$template->array_data['ERROR_DATE_FORMAT'] = '';
	$template->array_data['ERROR_LOCATION'] = '';
	$template->array_data['ERROR_ICQ'] = '';
	$template->array_data['ERROR_JABBER'] = '';
	$template->array_data['ERROR_DESC'] = '';
	$template->array_data['ERROR_AVATAR_UPLOAD'] = '';
	$_SESSION['info_txt'] = '';
	if ($work->check_get('id', true, true, '^[0-9]+\$', true))
	{
		$tmp_user = new user($_GET['id']);
		if (!$tmp_user->id)
		{
			$_SESSION['error'] = true;
			$_SESSION['error_txt'] = $language->error['no_this_user'];
			$tmp_user = $user;
		}
		else
		{
			$upload_avatar = false;
			$edit_username = false;
			$edit_profile = false;
			$edit_email = false;
			if ($tmp_user->id != $user->id && $user->permission['edit_all_profile'])
			{
				if ($user->permission['allow_upload_avatar']) $upload_avatar = true;
				if ($user->permission['allow_change_name']) $edit_username = true;
				$edit_profile = true;
				$edit_email = true;
			}
			else if ($tmp_user->id == $user->id && ($tmp_user->permission['edit_own_profile'] || $tmp_user->permission['edit_all_profile']))
			{
				if ($tmp_user->permission['allow_upload_avatar']) $upload_avatar = true;
				if ($tmp_user->permission['allow_change_name']) $edit_username = true;
				$edit_profile = true;
				$edit_email = true;
			}
			if ($work->check_post('language', true, true, REG_LOGIN) && $edit_profile && $_POST['language'] != $tmp_user->data['language'])
			{
				$tmp_user->update_data('language', $_POST['language']);
				if ($tmp_user->id == $user->id)
				{
					$user->data['language'] = $tmp_user->data['language'];
					$language->language($work->config['site_dir'], $user->data['language'], 'profile');
					$local_menu = $language->local_menu();
				}
			}
			else if ($work->check_post('language', true, true, REG_LOGIN) && $_POST['language'] != $tmp_user->data['language'])
			{
				$template->array_data['ERROR_LANGUAGE'] = ERROR_FIELD;
			}
			if ($work->check_post('email', true, true, REG_EMAIL) && $edit_email && $edit_profile && $tmp_user->data['email'] != $_POST['email'])
			{
				$db->select('id', DB_USER, TBL_USERS);
				$db->where('`email` = "' . $_POST['email'] . '"');
				$db->query();
				if (!$db->res_row())
				{
					$db->clean();
					$activated_code = false;
					if ($tmp_user->update_data('email', $_POST['email']))
					{
						$_SESSION['info'] = true;
						$_SESSION['info_txt'] = $language->profile['change_email'];
						if ($work->config['requires_activation'])
						{
							$activated_code = $user->gen_activated_code($tmp_user->id);
						}
					}
					else if ($tmp_user->data['email'] != $_POST['email'])
					{
						$template->array_data['ERROR_EMAIL'] = ERROR_FIELD;
					}
					if ($activated_code)
					{
						$mail_array = array();
						$url_activated = $work->config['site_url'] . '?action=activate&login=' . $tmp_user->data['login'] . '&email=' . $tmp_user->data['email'];
						$mail_array['USER_NAME'] = $tmp_user->data['name'];
						$mail_array['USER_EMAIL'] = $tmp_user->data['email'];
						$mail_array['URL_ACCOUNT_ACTIVATE'] = $url_activated . '&activated_code=' . $activated_code;
						$mail_array['URL_ACTIVATE'] = $url_activated;
						$mail_array['USER_LOGIN'] = $tmp_user->data['login'];
						$mail_array['USER_CODE_ACTIVATED'] = $activated_code;
						$mail_array['USER_PASS'] = '********';
						$mail_array['SERVER_URL'] = $work->config['site_url'];
						$mail_array['SERVER_NAME'] = $work->config['title'];
						$language->local_mail($tmp_user->data['email'], $tmp_user->data['login'], 'confirm_register', $mail_array);
						$_SESSION['info'] = true;
						$_SESSION['info_txt'] = sprintf($language->main['send_activated_code_ch_email'], $tmp_user->data['name'], $work->filt_email($tmp_user->data['email']));
						$_SESSION['user_login'] = $tmp_user->data['login'];
						$user->last_active();
						if ($tmp_user->id === $user->id) $user->clean_data_of_user();
						header('Location: ' . $work->config['site_url']);
						die('HACK!');
					}
				}
				else
				{
					$db->clean();
					$_SESSION['error'] = true;
					$_SESSION['error_txt'] = sprintf($language->error['email_already_used'], $work->filt_email($_POST['email']));
					$template->array_data['ERROR_EMAIL'] = ERROR_FIELD;
				}
			}
			else if ($tmp_user->data['email'] != $_POST['email'] && !empty($_POST['email']))
			{
				$template->array_data['ERROR_EMAIL'] = ERROR_FIELD;
			}
			if ($work->check_post('pass', true, true, REG_NAME) && $work->check_post('re_pass', true, true, REG_NAME) && $edit_profile)
			{
				if ($_POST['pass'] == $_POST['re_pass'])
				{
					$new_pass = md5(sha1($tmp_user->data['login'] . $_POST['pass']));
					if ($tmp_user->update_data('pass', $new_pass))
					{
						$_SESSION['info'] = true;
						$mail_array['USER_NAME'] = $tmp_user->data['name'];
						$mail_array['USER_LOGIN'] = $tmp_user->data['login'];
						$mail_array['USER_PASS'] = $_POST['pass'];
						$mail_array['SERVER_URL'] = $work->config['site_url'];
						$mail_array['SERVER_NAME'] = $work->config['title'];
						$language->local_mail($tmp_user->data['email'], $tmp_user->data['login'], 'send_pass', $mail_array);
						$_SESSION['info_txt'] .= $language->profile['change_pass'];
					}
					else
					{
						$_SESSION['error'] = true;
						$_SESSION['error_txt'] = $language->error['unknown'];
					}
				}
				else
				{
					$_SESSION['error'] = true;
					$_SESSION['error_txt'] = $language->error['pass_re_pass'];
					$template->array_data['ERROR_PASS'] = ERROR_FIELD;
					$template->array_data['ERROR_RE_PASS'] = ERROR_FIELD;
				}
			}
			else if (!empty($_POST['pass']) && empty($_POST['re_pass']))
			{
				$template->array_data['ERROR_RE_PASS'] = ERROR_FIELD;
			}
			else if (empty($_POST['pass']) && !empty($_POST['re_pass']))
			{
				$template->array_data['ERROR_PASS'] = ERROR_FIELD;
			}
			if (!empty($_POST['pass']) && !$work->check_post('pass', true, true, REG_NAME))
			{
				$template->array_data['ERROR_PASS'] = ERROR_FIELD;
			}
			if (!empty($_POST['re_pass']) && !$work->check_post('re_pass', true, true, REG_NAME))
			{
				$template->array_data['ERROR_RE_PASS'] = ERROR_FIELD;
			}
			if ($work->check_post('name', true, true, REG_NAME) && $edit_username && $_POST['name'] != $tmp_user->data['name'])
			{
				$tmp_user->update_data('name', $work->clean_field($_POST['name']));
			}
			else if ($work->check_post('name', true, true, REG_NAME) && $_POST['name'] != $tmp_user->data['name'])
			{
				$template->array_data['ERROR_NAME'] = ERROR_FIELD;
			}
			if ($work->check_post('theme', true, true, REG_LOGIN) && $edit_profile && $_POST['theme'] != $tmp_user->data['theme'])
			{
				$tmp_user->update_data('theme', $_POST['theme']);
				if ($tmp_user->id == $user->id)
				{
					$user->data['theme'] = $tmp_user->data['theme'];
					$template->template($work->config['site_url'], $work->config['site_dir'], $user->data['theme'], $user->data['faction']);
				}
			}
			else if ($work->check_post('theme', true, true, REG_LOGIN) && $_POST['theme'] != $tmp_user->data['theme'])
			{
				$template->array_data['ERROR_THEME'] = ERROR_FIELD;
			}
			if ($work->check_post('date_format', true, true, REG_NAME) && $edit_profile && $_POST['date_format'] != $tmp_user->data['date_format'])
			{
				$tmp_user->update_data('date_format', $work->clean_field($_POST['date_format']));
				if ($tmp_user->id == $user->id)
				{
					$user->data['date_format'] = $tmp_user->data['date_format'];
				}
			}
			else if ($work->check_post('date_format', true, true, REG_NAME) && $_POST['date_format'] != $tmp_user->data['date_format'])
			{
				$template->array_data['ERROR_DATE_FORMAT'] = ERROR_FIELD;
			}
			if ($work->check_post('location', true, false, REG_NAME) && $edit_profile && $_POST['location'] != $tmp_user->data['location'])
			{
				$tmp_user->update_data('location', $work->clean_field($_POST['location']));
			}
			else if ($work->check_post('location', true, false, REG_NAME) && $_POST['location'] != $tmp_user->data['location'])
			{
				$template->array_data['ERROR_LOCATION'] = ERROR_FIELD;
			}
			if ($work->check_post('icq', true, false, '^[0-9]*\$') && $edit_profile && $_POST['icq'] != $tmp_user->data['icq'])
			{
				$tmp_user->update_data('icq', $work->clean_field($_POST['icq']));
			}
			else if ($work->check_post('icq', true, false, '^[0-9]*\$') && $_POST['icq'] != $tmp_user->data['icq'])
			{
				$template->array_data['ERROR_ICQ'] = ERROR_FIELD;
			}
			if ($work->check_post('jabber', true, false, REG_EMAIL) && $edit_profile && $_POST['jabber'] != $tmp_user->data['jabber'])
			{
				$tmp_user->update_data('jabber', $_POST['jabber']);
			}
			else if ($work->check_post('jabber', true, false, REG_EMAIL) && $_POST['jabber'] != $tmp_user->data['jabber'])
			{
				$template->array_data['ERROR_JABBER'] = ERROR_FIELD;
			}
			if ($work->check_post('desc', true, false, REG_NAME) && $edit_profile && $_POST['desc'] != $tmp_user->data['desc'])
			{
				$tmp_user->update_data('desc', $work->clean_field($_POST['desc']));
			}
			else if ($work->check_post('desc', true, false, REG_NAME) && $_POST['desc'] != $tmp_user->data['desc'])
			{
				$template->array_data['ERROR_DESC'] = ERROR_FIELD;
			}
			if ($work->check_post('delete_avatar', true, true)) $tmp_user->del_avatar();
			$upload_data = $tmp_user->upload_avatar('avatar_upload');
			if ($upload_data !== false)
			{
				if ($upload_data === 'err_size')
				{
					$_SESSION['error'] = true;
					$_SESSION['error_txt'] = $language->profile['error_upload_size'];
				}
				else if ($upload_data === 'err_type')
				{
					$_SESSION['error'] = true;
					$_SESSION['error_txt'] = $language->profile['error_upload_type'];
				}
				else if ($upload_data === 'err_w_or_h')
				{
					$_SESSION['error'] = true;
					$_SESSION['error_txt'] = $language->profile['error_upload_w_h'];
				}
				else if ($tmp_user->id == $user->id)
				{
					$user->data['avatar'] = $user->get_avatar();
				}
			}
		}
	}
	else
	{
		$tmp_user = $user;
	}
	if ($tmp_user->id)
	{
		$template->template_file = 'profile.html';
		$title .= ' - ' . sprintf($language->title['profile'], $tmp_user->data['name']);
		$template->array_data['L_SERVER_NAME'] = $work->config['description'];
		$template->array_data['L_EDIT_PROFILE'] = sprintf($language->title['profile'], $tmp_user->data['name']);
		$template->array_data['L_MAIN_DATA'] = $language->profile['main_data'];
		$template->array_data['L_LOGIN'] = $language->main['user_login'];
		$template->array_data['L_EMAIL'] = $language->main['user_email'];
		$template->array_data['L_USERNAME'] = $language->main['user_name'];
		$template->array_data['L_PASSWORD'] = $language->main['user_pass'];
		$template->array_data['L_RE_PASSWORD'] = $language->main['user_re_pass'];
		if ($work->config['requires_activation']) $template->array_data['L_IF_CHANGE_EMAIL'] = $language->profile['if_change_email'];
		else $template->array_data['L_IF_CHANGE_EMAIL'] = '';
		$template->array_data['L_IF_CHANGE_PASS'] = $language->profile['if_change_pass'];
		$template->array_data['L_LANGUAGE'] = $language->main['user_language'];
		$template->array_data['L_THEME'] = $language->main['user_theme'];
		$template->array_data['L_GROUP'] = $language->main['user_group'];
		$template->array_data['L_DATA_REGIST'] = $language->profile['data_regist'];
		$template->array_data['L_ACTIVATED'] = $language->profile['activated'];
		$template->array_data['L_BANNED'] = $language->profile['banned'];
		$template->array_data['L_DATE_FORMAT'] = $language->profile['date_format'];
		$template->array_data['L_DATE_FORMAT_DESC'] = $language->profile['date_format_desc'];
		$template->array_data['L_CONTACT_DATA'] = $language->profile['contact_data'];
		$template->array_data['L_LOCATION'] = $language->profile['location'];
		$template->array_data['L_ICQ'] = $language->profile['icq'];
		$template->array_data['L_JABBER'] = $language->profile['jabber'];
		$template->array_data['L_DESC'] = $language->profile['desc'];
		$template->array_data['L_GAME_DATA'] = $language->profile['game_data'];
		$template->array_data['L_FACTION'] = $language->main['user_faction'];
		$template->array_data['L_ALLIANCE'] = $language->main['user_alliance'];
		$template->array_data['L_AVATAR'] = $language->profile['avatar'];
		$template->array_data['L_GALLERY_AVATAR'] = $language->profile['gallery_avatar'];
		$template->array_data['L_DELETE_AVATAR'] = $language->profile['delete_avatar'];
		$template->array_data['L_CAN_UPLOAD'] = $language->profile['can_upload_avatar'];
		$template->array_data['L_OR'] = $language->profile['or'];
		$template->array_data['L_SAVE_PROFILE'] = $language->profile['save_profile'];
		$template->array_data['D_USERNAME'] = $tmp_user->data['name'];
		$template->array_data['D_LOGIN'] = $tmp_user->data['login'];
		$template->array_data['D_EMAIL'] = $tmp_user->data['email'];
		$template->array_data['D_FILT_EMAIL'] = $work->filt_email($tmp_user->data['email']);
		$languages = $work->get_languages();
		foreach ($languages as $key=>$value)
		{
			$template->array_data['ARRAY_LANGUAGE'][$key]['ID_LANGUAGE'] = $value['value'];
			$template->array_data['ARRAY_LANGUAGE'][$key]['D_LANGUAGE_NAME'] = $value['name'];
			$template->array_data['ARRAY_LANGUAGE'][$key]['D_SELECTD'] = '';
			if ($work->check_post('language', true, true, REG_LOGIN))
			{
				if ($_POST['language'] == $value['value']) $template->array_data['ARRAY_LANGUAGE'][$key]['D_SELECTD'] = ' selected="selected"';
			}
		}
		$themes = $work->get_themes();
		foreach ($themes as $key=>$value)
		{
			$template->array_data['ARRAY_THEME'][$key]['ID_THEME'] = $value;
			$template->array_data['ARRAY_THEME'][$key]['D_THEME_NAME'] = $value;
			$template->array_data['ARRAY_THEME'][$key]['D_SELECTD'] = '';
			if ($work->check_post('theme', true, true, REG_LOGIN))
			{
				if ($_POST['theme'] == $value) $template->array_data['ARRAY_THEME'][$key]['D_SELECTD'] = ' selected="selected"';
			}
		}
		$template->array_data['D_GROUP'] = $tmp_user->data['group_name'];
		$template->array_data['D_GROUP_DESC'] = $tmp_user->data['group_description'];
		$template->array_data['D_DATA_REGIST'] = $language->local_time($user->data['date_format'], $tmp_user->data['date_regist']);
		if ($tmp_user->data['activate']) $template->array_data['D_ACTIVATED'] = $language->main['yes'];
		else $template->array_data['D_ACTIVATED'] = $language->main['no'];
		if ($tmp_user->data['banned'])
		{
			$banned = $tmp_user->ban_reason($tmp_user->id);
			$template->array_data['D_BANNED'] = sprintf($language->profile['yes_banned'], $language->local_time($tmp_user->data['date_format'], $banned['date_ban']), $language->local_time($tmp_user->data['date_format'], $banned['term_ban']), $banned['reason_ban']);
		}
		else
		{
			$template->array_data['D_BANNED'] = $language->main['no'];
		}
		$template->array_data['D_DATE_FORMAT'] = $tmp_user->data['date_format'];
		$template->array_data['D_LOCATION'] = $tmp_user->data['location'];
		$template->array_data['D_ICQ'] = $tmp_user->data['icq'];
		$template->array_data['D_JABBER'] = $tmp_user->data['jabber'];
		$template->array_data['D_DESC'] = $tmp_user->data['desc'];
		$template->array_data['D_FACTION'] = $language->local_faction($tmp_user->data['faction_name']);
		if ($tmp_user->data['alliance'])
		{
			$template->array_data['D_ALLIANCE'] = $tmp_user->alliance['name'] . '&nbsp;(' . $tmp_user->alliance['title'] . ')';
		}
		else
		{
			$template->array_data['D_ALLIANCE'] = '-';
		}
		$avatar = $tmp_user->get_avatar(true);
		$template->array_data['D_AVATAR_WIDTH'] = $avatar['width'] . 'px';
		$template->array_data['D_AVATAR_HEIGHT'] = $avatar['height'] . 'px';
		$template->array_data['D_MAX_AVATAR_SIZE'] = $work->config['max_avatar_size'];
		$template->array_data['U_EDIT_PROFILE'] = $work->config['site_url'] . '?action=profile&amp;id=' . $tmp_user->id;
		$template->array_data['U_AVATAR'] = $work->config['site_url'] . '?action=avatar&amp;id=' . $user->id;
		$template->array_data['U_GALLERY_AVATAR'] = '#'; // Галерея будет реализована позже
		$template->array_data['IF_UPLOAD_AVATAR'] = false;
		$template->array_data['IF_EDIT_PROFILE'] = false;
		$template->array_data['IF_EDIT_USERNAME'] = false;
		$template->array_data['IF_VIEW_LOGIN_EMAIL'] = false;
		$template->array_data['IF_ACTIVATED_NEED'] = $work->config['requires_activation'];
		if ($tmp_user->id != $user->id && $user->permission['edit_all_profile'])
		{
			if ($user->permission['allow_upload_avatar']) $template->array_data['IF_UPLOAD_AVATAR'] = true;
			if ($user->permission['allow_change_name']) $template->array_data['IF_EDIT_USERNAME'] = true;
			$template->array_data['IF_EDIT_PROFILE'] = true;
			$template->array_data['IF_VIEW_LOGIN_EMAIL'] = true;
		}
		else if ($tmp_user->id == $user->id && ($tmp_user->permission['edit_own_profile'] || $tmp_user->permission['edit_all_profile']))
		{
			if ($tmp_user->permission['allow_upload_avatar']) $template->array_data['IF_UPLOAD_AVATAR'] = true;
			if ($tmp_user->permission['allow_change_name']) $template->array_data['IF_EDIT_USERNAME'] = true;
			$template->array_data['IF_EDIT_PROFILE'] = true;
			$template->array_data['IF_VIEW_LOGIN_EMAIL'] = true;
		}
		else if ($tmp_user->id == $user->id)
		{
			$template->array_data['IF_VIEW_LOGIN_EMAIL'] = true;
		}
		$template->array_data['IF_NO_EDIT_PROFILE'] = !$template->array_data['IF_EDIT_PROFILE'];
	}
	else
	{
		$_SESSION['error'] = true;
		$_SESSION['error_txt'] = $language->error['no_this_user'];
		$user->last_active();
		header('Location: ' . $work->config['site_url']);
		die('HACK!');
	}
}
else
{
	$_SESSION['error'] = true;
	$_SESSION['error_txt'] = $language->error['no_out_server'];
	$user->last_active();
	header('Location: ' . $work->config['site_url']);
	die('HACK!');
}
?>
